#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/14 09:50
# @Author  : 王凯
# @File    : guangdong_illegal.py
# @Project : spider-man
import re

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider
from utils.tools import parse_url_params


class GuangdongIllegalSpider(BaseTaxIllegalSpider):
    name: str = "guangdong_illegal"
    province: str = "广东"
    url: str = "https://guangdong.chinatax.gov.cn/siteapps/webpage/gdtax/zdsswfaj/index.jsp"  # 取首页

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.guangdong.chinatax.gov.cn:8443"})
        url = "https://guangdong.chinatax.gov.cn/siteapps/webpage/gdtax/zdsswfaj/query.jsp"
        for i in []:
            data = {
                "pageSize": "10",
                "pageNo": "1",
                "taxNature": "",
                "nsr_mc": f"{i}",
                "nsr_sbh": "",
                "fddbr_xm": "",
                "searchway": "02",
                "zcdz": "",
                "zzjgdm": "",
                "fddbrzjhm": "",
                "cwfzrxm": "",
                "cwfzrzjhm": "",
            }
            yield self.FormRequest(url, formdata=data, method="POST", callback=self.parse_list)

    def parse_list(self, response, **kwargs):
        yield from self.parse_manuscript(response, **kwargs)
        page_num = response.xpath(".").re_first(r"var maxPage\s*=(\d+)")
        if page_num:
            page_num = int(page_num)
            root_url, params = parse_url_params(response.request.body.decode())
            for page in range(2, page_num + 1):
                url = "http://guangdong.chinatax.gov.cn/siteapps/webpage/gdtax/zdsswfaj/query.jsp"
                yield self.FormRequest(
                    url, formdata={**params, "pageNo": page}, method="POST", callback=self.parse_manuscript
                )

    def parse_manuscript(self, response, **kwargs):
        manuscript_id_list = response.xpath('//*[@id="zdss_tb"]//a/@onclick')
        for manuscript_id in manuscript_id_list:
            url = "https://guangdong.chinatax.gov.cn/siteapps/webpage/gdtax/zdsswfaj/service.jsp"
            data = {"manuscriptId": manuscript_id.re_first(r"queryIllegalDetail\(\'(.*)\'\)")}
            yield self.FormRequest(url, formdata=data, method="POST", callback=self.parse_detail)

        if not manuscript_id_list:
            self.logger.debug(f"没有获取到数据 {response.request.body.decode()}")

    def parse_detail(self, response, **kwargs):
        item = NetTaxIllegalProItem()
        info = []
        for i in response.xpath('//*[@id="bbsTab"]/tr'):
            res = i.re(r"<td.*?>\s*(.*?)\s*</td>\s*<td.*?>\s*(.*?)\s*</td>")
            if res:
                info.append(res)

        info = dict(info)
        mapping = {
            "纳税人名称": "company_name",
            "纳税人识别号": "taxpayer_id",
            "注册地址": "address",
            "法定代表人或者负责人姓名、性别、证件名称及号码": "legal_representative",
            "违法期间法人代表或者负责人姓名、性别、证件名称及号码": "illegal_legal_representative",
            "负有直接责任的财务负责人姓名、性别、证件名称及号码": "resp_financial_head",
            "负有直接责任的中介机构信息及其从业人员信息": "resp_intermediary",
            "案件性质": "illegal_status",
            "主要违法事实": "illegal_facts",
            "相关法律依据及税务处理处罚情况": "basis_and_punishment",
        }

        for k, v in info.items():
            if mapping.get(k):
                setattr(item, mapping.get(k), re.sub("(<.*?>)", "", re.sub("(<!--.*?-->)", "", v)).strip())
        item.province = self.province
        year = re.findall(r"((?:20|19)\d{2})[-.年 /]", item.illegal_facts)
        if year:
            item.year = year[0]
        yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "guangdong_illegal"])
